using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

using ConsoleFx;
using ConsoleFx.Programs.Simple;
using ConsoleFx.Validators;

using Id3;

//BATCHRENAME [/default:Unknown] [/mask:*.mp3] [/prompt] <directory> ["{Artist} - {Title}"]
namespace BatchRename
{
    internal static class Runner
    {
        public static string DefaultForMissingValues = "Unknown";
        public static List<string> Masks = new List<string>();
        public static bool Prompt;

        public static string Directory;
        public static string Pattern = "{Artists} - {Title}";

        internal static int Run()
        {
            var app = new ConsoleProgram(Handler,
                includeHelpSupport: true,
                commandGrouping: CommandGrouping.DoesNotMatter,
                displayUsageOnError: true);
            try
            {
                app.AddOption("default", "d", expectedParameters: 1).ValidateWith(new StringValidator(1, int.MaxValue)).AssignTo(() => DefaultForMissingValues);
                app.AddOption("mask", "m", maxOccurences: int.MaxValue, minParameters: 1, maxParameters: int.MaxValue)
                    .ValidateWith(new StringValidator(1, int.MaxValue))
                    .AddToList(() => Masks);
                app.AddOption("prompt", "p").Flag(() => Prompt);
                app.AddArgument().ValidateWith(new PathValidator(PathType.Directory)).AssignTo(() => Directory);
                app.AddArgument(true).ValidateWith(new StringValidator(3, int.MaxValue)).AssignTo(() => Pattern);
                return app.Run();
            }
            catch (Exception ex)
            {
                return app.HandleError(ex);
            }
        }
        
        private static int Handler()
        {
            if (Masks.Count == 0)
                Masks.Add("*.mp3");
            IEnumerable<string> files = Masks.SelectMany(mask => System.IO.Directory.EnumerateFiles(Directory, mask, SearchOption.TopDirectoryOnly));
            var renamer = new FileNamer(Pattern);
            renamer.ResolveMissingData += (sender, args) => args.Value = DefaultForMissingValues;
            RenameSuggestions suggestions = renamer.GetSuggestions(files);
            foreach (RenameSuggestion suggestion in suggestions)
            {
                Console.WriteLine(suggestion.OriginalName);
                Console.ForegroundColor = StatusColorMapping[suggestion.Status];
                Console.WriteLine("    {0}", suggestion);
                Console.ResetColor();
            }
            return 0;
        }

        private static readonly Dictionary<RenameStatus, ConsoleColor> StatusColorMapping = new Dictionary<RenameStatus, ConsoleColor>(4) {
            { RenameStatus.CorrectlyNamed, ConsoleColor.Green },
            { RenameStatus.Renamed, ConsoleColor.Cyan },
            { RenameStatus.Cancelled, ConsoleColor.Yellow },
            { RenameStatus.Error, ConsoleColor.Red },
        };
    }
}